package com.huacai.spots.service.impl;

import java.util.Collections;
import java.util.List;

import com.huacai.common.utils.uuid.IdUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.huacai.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.huacai.spots.domain.SpotsTags;
import com.huacai.spots.mapper.SpotsMapper;
import com.huacai.spots.domain.Spots;
import com.huacai.spots.service.ISpotsService;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.util.CollectionUtils;

/**
 * 景点Service业务层处理
 *
 * @author hsj
 * @date 2025-05-11
 */
@Service
public class SpotsServiceImpl implements ISpotsService
{
    @Autowired
    private SpotsMapper spotsMapper;

    @Autowired
    private SqlSessionFactory sqlSessionFactory;

    /**
     * 查询景点
     *
     * @param spotsId 景点主键
     * @return 景点
     */
    @Override
    public Spots selectSpotsBySpotsId(String spotsId)
    {
        return spotsMapper.selectSpotsBySpotsId(spotsId);
    }

    /**
     * 查询景点列表
     *
     * @param spots 景点
     * @return 景点
     */
    @Override
    public List<Spots> selectSpotsList(Spots spots)
    {
        return spotsMapper.selectSpotsList(spots);
    }

    /**
     * 新增景点
     *
     * @param spots 景点
     * @return 结果
     */
    @Transactional
    @Override
    public int insertSpots(Spots spots)
    {
        spots.setSpotsId(IdUtils.fastSimpleUUID());
        int rows = spotsMapper.insertSpots(spots);
        insertSpotsTags(spots);
        return rows;
    }

    /**
     * 批量新增景点
     *
     * @param spotss 景点List
     * @return 结果
     */
    @Override
    public int batchInsertSpots(List<Spots> spotss)
    {
        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
        int count = 0;
        if (!CollectionUtils.isEmpty(spotss)) {
            try {
                for (int i = 0; i < spotss.size(); i++) {
                    int row = spotsMapper.insertSpots(spotss.get(i));
                    // 防止内存溢出，每100次提交一次,并清除缓存
                    boolean bool = (i >0 && i%100 == 0) || i == spotss.size() - 1;
                    if (bool){
                        sqlSession.commit();
                        sqlSession.clearCache();
                    }
                    count = i + 1;
                }
            }catch (Exception e){
                e.printStackTrace();
                // 没有提交的数据可以回滚
                sqlSession.rollback();
            }finally {
                sqlSession.close();
                return count;
            }
        }
        return count;
    }

    /**
     * 修改景点
     *
     * @param spots 景点
     * @return 结果
     */
    @Transactional
    @Override
    public int updateSpots(Spots spots)
    {
        spotsMapper.deleteSpotsTagsBySpotsId(spots.getSpotsId());
        insertSpotsTags(spots);
        return spotsMapper.updateSpots(spots);
    }

    /**
     * 批量删除景点
     *
     * @param spotsIds 需要删除的景点主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSpotsBySpotsIds(String[] spotsIds)
    {
        spotsMapper.deleteSpotsTagsBySpotsIds(spotsIds);
        return spotsMapper.deleteSpotsBySpotsIds(spotsIds);
    }

    /**
     * 删除景点信息
     *
     * @param spotsId 景点主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSpotsBySpotsId(String spotsId)
    {
        spotsMapper.deleteSpotsTagsBySpotsId(spotsId);

        return spotsMapper.deleteSpotsBySpotsId(spotsId);
    }




    /** * 查询景点列表（包括景点标签）
     * @param spots
     * @return
     */
    @Override
    public List<Spots> selectSptosOrTagList(Spots spots) {
        return spotsMapper.selectSptosOrTagList(spots);
    }


    /**
     * 不分页查询景点列表
     * @return
     */
    @Override
    public List<Spots> selectSpotsByList() {
        return spotsMapper.selectSpotsByList();
    }

    /**
     * 新增景点标签信息
     *
     * @param spots 景点对象
     */
    public void insertSpotsTags(Spots spots)
    {
        List<SpotsTags> spotsTagsList = spots.getSpotsTagsList();
        String spotsId = spots.getSpotsId();
        if (StringUtils.isNotNull(spotsTagsList))
        {
            List<SpotsTags> list = new ArrayList<SpotsTags>();
            for (SpotsTags spotsTags : spotsTagsList)
            {
                spotsTags.setSpotsId(spotsId);
                list.add(spotsTags);
            }
            if (list.size() > 0)
            {
                spotsMapper.batchSpotsTags(list);
            }
        }
    }
}
